<template>
    <div class="comments">
        <Search @searchData="getCommentsData" @resetData="getCommentsData" :placeholder="placeholder"></Search>
        <AddAndDel @delsByIds="delsByIds"></AddAndDel>
        <el-card>
            <!-- row-key="id" -->
            <!-- :tree-props="{children: 'children', hasChildren: 'hasChildren'}" -->
            <el-table ref="multipleTable" class="custom-table" height="450px"
                :default-sort="{ prop: 'date', order: 'descending' }"
                :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" :data="tableData" tooltip-effect="dark"
                style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="id" label="序号" width="100" sortable>
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column>
                <el-table-column prop="fromUserName" label="评论用户" width="130" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="content" label="评论内容" width="300" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="title" label="评论政策" width="300" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="createdDt" label="评论时间">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" style="color:red" @click="handleDelete(scope.row)"
                            plain>删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :paginationData="paginationData" @getPaginationData="getCommentsData"></Pagination>
        </el-card>
    </div>
</template>
<script>
import Search from '../../components/search/index.vue'
import AddAndDel from '../../components/add_and_del/index.vue'
import Pagination from '../../components/pagination/index.vue'
export default {
    name: 'Comments',
    components: {
        Search,
        AddAndDel,
        Pagination
    },
    mixins: [],
    props: {

    },
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            paginationData: {},
            placeholder: '请输入评论内容',
            defaultValue: 'default'
        }
    },
    computed: {
        multipleSelectionToObj() {
            return this.multipleSelection.reduce((obj, key) => {
                obj[key] = this.defaultValue
                return obj
            }, {})
        }
    },
    watch: {

    },
    mounted() {
        this.getCommentsData()
    },
    methods: {
        // 获取评论列表
        // /commentInfo/commentPage
        getCommentsData(obj = { currentPage: 1, pageSize: 10, keyword: '' }) {
            this.request.post('/commentInfo/commentPage', obj).then(res => {
                if (res.code == 20000) {
                    this.tableData = res.data.list
                    this.paginationData = res.data
                }
            })
        },
        //批量删除
        handleSelectionChange(selectedData) {
            // 被选中的数据的id
            this.multipleSelection = selectedData.map(item => item.id);

            this.$bus.$emit('multipleSelection', this.multipleSelection)

        },
        // 批量删除
        delsByIds() {
            this.$confirm('此操作将删除多条评论, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.request.delete('/commentInfo/commentDelBatch', { data: this.multipleSelection }).then(res => {
                    if (res.code == 20000) {
                        this.getCommentsData()
                        this.$message({
                            type: 'success',
                            message: '删除成功~'
                        })
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });

            });
        },
        // 删除评论
        handleDelete(row) {
            this.$confirm('此操作将删除该评论, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.request.delete(`/commentInfo/commentDel?id=` + row.id).then(res => {
                    if (res.code == 20000) {
                        this.getCommentsData()
                        this.$message({
                            type: 'success',
                            message: '删除成功~'
                        })

                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        }
    }
};
</script>
<style lang='less' scoped>
.comments {
    .el-table {
        font-size: 12px;
    }

    .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #11A983 !important;
        color: #FFF;
    }

}
</style>